﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Point2d.Geometry;

namespace Point2d.Graphics
{
    public class Entity
    {
        public enum EntityType
        {
            ePoint = 1,
            eLineStrip,
        }

        protected Pen _pen = null;
        protected Color _color = Color.Black;
        protected List<Point2> _points = new List<Point2>();
        protected GraphicsManager _graphicsManager = null;

        public Entity(GraphicsManager manager, Color color)
        {
            _graphicsManager = manager;
            _color = color;
            _pen = new Pen(_color);
        }

        public virtual void Draw(System.Drawing.Graphics graphics)
        {
        }

        public Point2 GetPoint(int index)
        {
            if (index < 0 || index >= _points.Count)
            {
                return null;
            }

            return _points[index];
        }

        public int GetCount()
        {
            return _points.Count;
        }

        public void AddPoint(float x, float y)
        {
            Point2 point = new Point2();
            point._x = x;
            point._y = y;
            _points.Add(point);
        }

        public void SetPoint(int index, float x, float y)
        {
            if (index < 0 || index >= _points.Count)
            {
                return;
            }
            _points[index].Set(x, y);
        }

        public void DrawLine(System.Drawing.Graphics graphics, Point2 from, Point2 to)
        {
            float _fromX = 0.0f;
            float _fromY = 0.0f;
            _graphicsManager.GetPointFromWorld2View(from, ref _fromX, ref _fromY);

            float _toX = 0.0f;
            float _toY = 0.0f;
            _graphicsManager.GetPointFromWorld2View(to, ref _toX, ref _toY);

            graphics.DrawLine(_pen, _fromX, _fromY, _toX, _toY);
        }

        public void DrawPoint(System.Drawing.Graphics graphics, Point2 point, float pointRadius)
        {
            float pointOfViewX = 0.0f;
            float pointOfViewY = 0.0f;
            _graphicsManager.GetPointFromWorld2View(point, ref pointOfViewX, ref pointOfViewY);
            graphics.FillRectangle(GraphicsManager._graphicsBrush,
                (int)(pointOfViewX - pointRadius),
                (int)(pointOfViewY - pointRadius),
                pointRadius * 2,
                pointRadius * 2);
        }
    }
}
